<template>
  <div class="dashboard-editor-container">

    <el-tooltip class="item" effect="dark" content="测试环境与线上环境的切换，默认为线上环境，切换后需要刷新页面生效。" placement="top-start">
      <el-switch v-model="value" active-color="#13ce66" @change="chang_env" inactive-color="#ff4949" active-value="1"
                 active-text="线上环境" inactive-value="0" inactive-text="测试环境"
      >
      </el-switch>
    </el-tooltip>
    <panel-group @handleSetLineChartData="handleSetLineChartData"/>


    <el-row :gutter="8">
      <el-col :xs="{ span: 24 }" :sm="{ span: 24 }" :md="{ span: 24 }" :lg="{ span: 10 }" :xl="{ span: 10 }"
              style=" margin-bottom: 30px"
      >
        <transaction-table/>
      </el-col>
      <el-col :xs="{ span: 24 }" :sm="{ span: 24 }" :md="{ span: 24 }" :lg="{ span: 14 }" :xl="{ span: 14 }"
              style="margin-bottom: 30px"
      >
        <box-card/>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import PanelGroup from './dashboard/PanelGroup'
import LineChart from './dashboard/LineChart'
import RaddarChart from './dashboard/RaddarChart'
import PieChart from './dashboard/PieChart'
import BarChart from './dashboard/BarChart'
import { Notification } from 'element-ui'
import GithubCorner from '@/components/GithubCorner'
import TransactionTable from './dashboard/TransactionTable'
import TodoList from './dashboard/TodoList'
import BoxCard from './dashboard/BoxCard'

import axios from 'axios'

import { getEnv, setEnv } from '@/utils/auth'
import RuoYiDoc from '@/components/RuoYi/Doc/index.vue'

const lineChartData = {
  newVisitis: {
    expectedData: [100, 120, 161, 134, 105, 160, 165],
    actualData: [120, 82, 91, 154, 162, 140, 145]
  },
  messages: {
    expectedData: [200, 192, 120, 144, 160, 130, 140],
    actualData: [180, 160, 151, 106, 145, 150, 130]
  },
  purchases: {
    expectedData: [80, 100, 121, 104, 105, 90, 100],
    actualData: [120, 90, 100, 138, 142, 130, 130]
  },
  shoppings: {
    expectedData: [130, 140, 141, 142, 145, 150, 160],
    actualData: [120, 82, 91, 154, 162, 140, 130]
  }
}

export default {
  name: 'Index',
  components: {
    RuoYiDoc,
    PanelGroup,
    LineChart,
    RaddarChart,
    PieChart,
    BarChart,

    TransactionTable,
    TodoList,
    BoxCard
  },
  data() {
    return {
      value: '1',
      lineChartData: lineChartData.newVisitis
    }
  },
  methods: {
    //切换环境
    chang_env(e) {
      //获取到环境变量
      if (e === 1) {
        axios.get(process.env.VUE_APP_BASE_API + '/captchaImage', { timeout: 1000 }).then(
          res => {
            setEnv(e)
            console.log(e)
            //强制刷新
            window.location.reload()
          }
        ).catch(err => {
          Notification.error({
            title: '切换环境错误'
          })
          this.value = '0'
          console.log(this.value)

        })
      } else {
        axios.get(process.env.VUE_APP_TEST_API + '/captchaImage', { timeout: 1000 }).then(
          res => {
            setEnv(e)
            console.log(e)
            //强制刷新
            window.location.reload()
          }
        ).catch(err => {
          this.value = '1'
          Notification.error({
            title: '切换环境错误'
          })
        })
      }

    },
    handleSetLineChartData(type) {
      this.lineChartData = lineChartData[type]
    }
  },
  created() {
    if (getEnv()) {
      this.value = getEnv()
    } else {
      setEnv(0)
    }
  }
}
</script>

<style lang="scss" scoped>
.dashboard-editor-container {
  padding: 32px;
  background-color: rgb(240, 242, 245);
  position: relative;

  .chart-wrapper {
    background: #fff;
    padding: 16px 16px 0;
    margin-bottom: 32px;
  }
}

@media (max-width: 1024px) {
  .chart-wrapper {
    padding: 8px;
  }
}
</style>
